
package com.yzh.pinboard.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.yzh.pinboard.CameraPreview.CameraView;
import com.yzh.pinboard.CameraPreview.DisplayOrientationDetector;
import com.yzh.pinboard.CameraPreview.ViewCompat2;
import com.yzh.pinboard.R;
import com.yzh.pinboard.utils.DensityUtil;

/**
 * Created by Administrator on 2017/3/6.
 */

public class CameraDialog extends Dialog implements View.OnClickListener {
    private CameraCallBack mCameraCallBack;
    private Context mContext;
    private CameraView mCameraView;
    private Button mBtnCancel;
    private Button mBtnShot;
    private Button mBtnSave;
    private ImageView mImageView;
    private Bitmap mBitmap;

    public CameraDialog(@NonNull Context context) {
        super(context, R.style.Dialog_Camera);
        mContext = context;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_camera);
        mCameraView = (CameraView) findViewById(R.id.camera);

        mBtnCancel = (Button) findViewById(R.id.cancel);
        mBtnShot = (Button) findViewById(R.id.shot);
        mBtnSave = (Button) findViewById(R.id.save);
        mImageView = (ImageView) findViewById(R.id.iv);
        mCameraView.addCallback(mCallback);
        mBtnCancel.setOnClickListener(this);
        mBtnShot.setOnClickListener(this);
        mBtnSave.setOnClickListener(this);

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    @Override
    public void show() {
        super.show();
        try {
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.width = DensityUtil.getScreenWidth(mContext);
            params.height = DensityUtil.getScreenHeight(mContext) + DensityUtil
                    .getNavigationBarHeight(mContext);
            getWindow().setAttributes(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mCameraView.start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mCameraView.stop();
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.shot) {
            mCameraView.takePicture();
        } else if (i == R.id.cancel) {
            mBtnSave.setVisibility(View.GONE);
            mBtnCancel.setVisibility(View.GONE);
            mBtnShot.setVisibility(View.VISIBLE);
            mImageView.setVisibility(View.GONE);
            mBitmap = null;
            mCameraView.start();


        } else if (i == R.id.save) {
            if (mCameraCallBack != null) {
                mCameraCallBack.shot(mBitmap);
            }
            dismiss();
        }
    }


    private static Bitmap rotateBitmap(Bitmap bitmap, int degrees) {
        if (degrees == 0 || null == bitmap) {
            return bitmap;
        }
        Matrix matrix = new Matrix();
        matrix.setRotate(degrees, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
        Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true);
        bitmap.recycle();
        return bmp;
    }

    private CameraView.Callback mCallback = new CameraView.Callback() {
        @Override
        public void onCameraOpened(CameraView cameraView) {
            super.onCameraOpened(cameraView);
        }

        @Override
        public void onCameraClosed(CameraView cameraView) {
            super.onCameraClosed(cameraView);
        }

        @Override
        public void onPictureTaken(CameraView cameraView, byte[] data) {
            super.onPictureTaken(cameraView, data);
            Display display = ViewCompat2.getDisplay(mCameraView);
            int degree = DisplayOrientationDetector.DISPLAY_ORIENTATIONS.get(display
                    .getRotation());

            Bitmap oriBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            mBitmap = rotateBitmap(oriBitmap, 90 - degree);
            mImageView.setImageBitmap(mBitmap);

            mImageView.setVisibility(View.VISIBLE);
            mBtnSave.setVisibility(View.VISIBLE);
            mBtnCancel.setVisibility(View.VISIBLE);
            mBtnShot.setVisibility(View.GONE);
        }
    };

    public interface CameraCallBack {
        void shot(Bitmap bitmap);
    }

    public void setCameraCallBack(CameraCallBack cameraCallBack) {
        mCameraCallBack = cameraCallBack;
    }

}
